package org.yty.travelagencymongodb.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.yty.travelagencymongodb.model.Entity.TourGroup;
import org.yty.travelagencymongodb.model.Entity.TourGroupTraveller;
import org.yty.travelagencymongodb.model.Entity.Traveller;
import org.yty.travelagencymongodb.repository.TourGroupTravellerRepository;
import org.yty.travelagencymongodb.service.TourGroupService;
import org.yty.travelagencymongodb.service.TourGroupTravellerService;
import org.yty.travelagencymongodb.service.TravellerService;

import java.util.List;

@Service
@RequiredArgsConstructor
public class TourGroupTravellerServiceImpl implements TourGroupTravellerService {
    private final TourGroupTravellerRepository tourGroupTravellerRepository;
    private final MongoTemplate mongoTemplate;
    @Lazy
    @Autowired
    private TourGroupService tourGroupService;
    @Lazy
    @Autowired
    private TravellerService travellerService;

    @Override
    public List<TourGroup> getTourGroupListByTravellerId(Long travellerId) {
        Criteria queryCriteria = Criteria.where("travellerId").is(travellerId);
        List<TourGroupTraveller> tourGroupTravellerList = this.mongoTemplate.find(new Query(queryCriteria), TourGroupTraveller.class);
        List<Long> tourGroupIdList = tourGroupTravellerList.stream().map(elem -> elem.getTourGroupId()).toList();
        return this.tourGroupService.getListByIdList(tourGroupIdList);
    }

    @Override
    public List<Traveller> getTravellerListByTourGroupId(Long tourGroupId) {
        Criteria queryCriteria = Criteria.where("tourGroupId").is(tourGroupId);
        List<TourGroupTraveller> tourGroupTravellerList = this.mongoTemplate.find(new Query(), TourGroupTraveller.class);
        List<Long> travellerIdList = tourGroupTravellerList.stream().map(elem -> elem.getTravellerId()).toList();
        return this.travellerService.getListByIdList(travellerIdList);
    }

    @Override
    public void deleteByTourGroupId(Long tourGroupId) {
        this.tourGroupTravellerRepository.deleteByTourGroupId(tourGroupId);
    }

    @Override
    public void deleteByTravellerId(Long travellerId) {
        this.tourGroupTravellerRepository.deleteByTravellerId(travellerId);
    }

    @Override
    public void deleteByTourGroupIdAndTravellerId(Long tourGroupId, Long travellerId) {
        this.tourGroupTravellerRepository.deleteByTourGroupIdAndTravellerId(tourGroupId,travellerId);
    }

    @Override
    public boolean save(TourGroupTraveller tourGroupTraveller) {
        tourGroupTraveller = this.tourGroupTravellerRepository.insert(tourGroupTraveller);
        return tourGroupTraveller != null;
    }
}
